package com.itcoon.cloud.framework.ex.task;/**
 * @author Athric Yu
 * 2024/11/26
 **/

import com.itcoon.cloud.framework.ex.config.ExRegistryConfigurer;
import com.itcoon.cloud.framework.ex.register.IErrorCodeRegister;
import com.itcoon.cloud.framework.model.BootstrapTask;
import com.itcoon.cloud.framework.utils.SpringUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 *
 *
 * @author Athric Yu
 * 2024/11/26
 **/
public class ErrorCodeRegisterTask implements BootstrapTask<Void> {

    private final IErrorCodeRegister errorCodeRegister;
    private final List<ExRegistryConfigurer> exRegistryConfigurers;

    public ErrorCodeRegisterTask(IErrorCodeRegister errorCodeRegister, List<ExRegistryConfigurer> exRegistryConfigurers) {
        this.errorCodeRegister = errorCodeRegister;
        this.exRegistryConfigurers = exRegistryConfigurers;
    }

    @Override
    public CompletableFuture<Void> get() {
        return CompletableFuture.runAsync(()->{
            errorCodeRegister.preRegister();
            Map<String, ExRegistryConfigurer> exConfigMap = SpringUtils.getBeansMap(ExRegistryConfigurer.class);
            exConfigMap.values().forEach(v->v.registerErrorCodes(errorCodeRegister));
            errorCodeRegister.postRegister();
        });
    }
}
